using System;
using System.Collections.Generic;
using System.Text;
using Helper;
using Microsoft.Xna.Framework;
namespace SpaceWolf.Particle3D
{
    public class ParticleManager
    {
        float starttime = 0;
        Vector3 position=Vector3.Zero;
        ExplosionParticleSystem explosionParticles;
        //ExplosionSmokeParticleSystem explosionSmokeParticles;

        //ParticleSystem smokePlumeParticles;
        // The explosions effect works by firing projectiles up into the
        // air, so we need to keep track of all the active projectiles.
        List<Projectile> projectiles = new List<Projectile>();
        
        float timeToNextProjectile = 0;

        public ParticleManager()
        {
            explosionParticles = new ExplosionParticleSystem();
            //explosionSmokeParticles = new ExplosionSmokeParticleSystem();
            //projectileTrailParticles = new ProjectileTrailParticleSystem();

            //smokePlumeParticles = new SmokePlumeParticleSystem();
        }


        public void Initialize()
        {
            explosionParticles.Initialize();
            //explosionSmokeParticles.Initialize();
            //projectileTrailParticles.Initialize();

            //smokePlumeParticles.Initialize();
        }

        public void StartNew(Vector3 Position)
        {
            starttime=0;
            position = Position;
        }
        /// <summary>
        /// Helper for updating the explosions effect.
        /// </summary>
        void UpdateExplosions()
        {
            timeToNextProjectile -= BaseGame.ElapsedTimeThisFrameInMilliseconds;
            if (timeToNextProjectile <= 0)
            {
                starttime++;
                // Create a new projectile once per second. The real work of moving
                // and creating particles is handled inside the Projectile class.
                projectiles.Add(new Projectile(explosionParticles,
                                               position));//,
                                               //projectileTrailParticles));
                timeToNextProjectile += 1000;
            }
            
        }

        /// <summary>
        /// Helper for updating the list of active projectiles.
        /// </summary>
        void UpdateProjectiles()
        {
            explosionParticles.Update();
            int i = 0;
            while (i < projectiles.Count)
            {
                if (!projectiles[i].Update(0.05f))
                {
                    // Remove projectiles at the end of their life.
                    projectiles.RemoveAt(i);
                }
                else
                {
                    // Advance to the next projectile.
                    i++;
                }
            }
        }

        public void Update()
        {
            //smokePlumeParticles.AddParticle(new Vector3(5, -10, 100f), Vector3.Zero);
            //smokePlumeParticles.Update();
            if (starttime < 3)
            {
                UpdateExplosions();
            }
            UpdateProjectiles();
            //explosionSmokeParticles.Update();
            //projectileTrailParticles.Update();
        }

        public void Render()
        {
            explosionParticles.Draw();
            //explosionSmokeParticles.Draw();
            //projectileTrailParticles.Draw();
            //smokePlumeParticles.Draw();
        }


        #region Test
        public static void TestParticle()
        {
            
            ParticleManager pm = new ParticleManager();
            TestGame.Start("Explosion Test",
                delegate // Init Code
              {
                  pm.Initialize();
                  pm.StartNew(new Vector3(5, -40, -100f));
              },
              delegate { pm.Update();},
              delegate // 3d render code
              {
                  Fonts.WriteText(2, 30,
                           "cam pos=" + BaseGame.CameraPos);
                  Fonts.WriteText(2, 70,
                           "totalMilliseconds" + pm.starttime);
                  
                  
                  pm.Render();
              });
        }
        #endregion
    }
}
